package com.wlcorp.forcazamis.dao.baseinfo.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.baseinfo.ICustomerDAO;
import com.wlcorp.forcazamis.domain.baseinfo.Customer;
import com.wlcorp.forcazamis.sys.util.Validator;

public class CustomerDAOImpl extends HibernateDaoSupport implements
		ICustomerDAO {

	@SuppressWarnings("unchecked")
	public List getCustomerList(final Customer customer, Long staffId) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select c from Customer c where c.deleteFlag=false ");
		if (null!=staffId&&staffId!=0) {
			queryString.append(" and c in (select elements(s.customers) from Staff s where s.id=").append(staffId).append(")");
		}
		if (null != customer) {
			if (Validator.checkValueIsValid(customer.getCustomerFullName())){
				queryString.append(" and c.customerFullName like '%").append(customer.getCustomerFullName()).append("%'");
			}
			if(Validator.checkValueIsValid(customer.getCustomerName())){
				queryString.append(" and c.customerName = '").append(customer.getCustomerName()).append("'");
			}
		}
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != customer && null != customer.getMinRownum()){
					query.setFirstResult(customer.getMinRownum());
					query.setMaxResults(customer.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}
	public Long getCustomerCount(Customer customer) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(customer) from Customer as customer where 1=1 and customer.deleteFlag=false");
		if(null != customer){
			if(Validator.checkValueIsValid(customer.getCustomerName())){
				queryString.append(" and customer.customerName like '%").append(customer.getCustomerName()).append("%'");
			}
			if(Validator.checkValueIsValid(customer.getCustomerName())){
				queryString.append(" and customer.customerName = '").append(customer.getCustomerName()).append("'");
			}
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
			
		});
	}
	public Customer getCustomerById(Long customerId) throws DaoException {
		return (Customer)getHibernateTemplate().get(Customer.class,customerId);
	}

	public void deleteCustomer(Customer customer) throws DaoException {
		customer.setDeleteFlag(true);
		getHibernateTemplate().update(customer);
	}

	public void saveCustomer(Customer customer) throws DaoException {
		getHibernateTemplate().saveOrUpdate(customer);
	}

}
